package com.topone.service.impl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Utils.JsonUtil;
import com.topone.Utils.MathUtil;
import com.topone.dto.OrderDTO;
import com.topone.service.OrderService;
import com.topone.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by Administrator on 2018/2/2,0002.
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {
    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService;

    private static final String ORDER_NAME="购买优惠券";

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest=new PayRequest();
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5); //支付方式
        log.info("【微信支付】payRequest={}",JsonUtil.toJson(payRequest));
        PayResponse payResponse=bestPayService.pay(payRequest);
        log.info("【微信支付】payResponse={}", JsonUtil.toJson(payResponse));

        return  payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1.验证签名
        //2.支付的状态
        //3.支付的金额
        //4.支付人（下单人==支付人）
        PayResponse payResponse=bestPayService.asyncNotify(notifyData);
        log.info("【微信支付】异步通知,payRequest={}",JsonUtil.toJson(payResponse));

        OrderDTO orderDTO=orderService.findOne(payResponse.getOrderId());
        //查询订单是否存在
        if(Objects.isNull(orderDTO)){
            log.error("【微信支付】异步通知,订单不存在 orderID＝{}",payResponse.getOrderId());
            throw new FanZoneException(ResultEnum.ORDER_NOT_EXIST);
        }
        //判断金额是否一致
        if(!MathUtil.equal(payResponse.getOrderAmount(),orderDTO.getOrderAmount().doubleValue())){
            log.error("【微信支付】异步通知,订单金额不一致 orderID＝{},微信通知金额＝{},系统定额＝{}"
                    ,payResponse.getOrderId(),payResponse.getOrderAmount(),orderDTO.getOrderAmount());
           throw new FanZoneException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }
        //判断支付人

        //修改订单的支付状态
        orderService.paid(orderDTO);
       return payResponse;
    }

    /**
     *
     * @param orderDTO
     * @return
     */
    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest=new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】request={}", JsonUtil.toJson(refundRequest));
        RefundResponse refundResponse= bestPayService.refund(refundRequest);
        log.info("【微信退款】refundResponse={}", JsonUtil.toJson(refundResponse));

        return refundResponse;
    }
}


